//
//  RCLoginPageVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-7-1.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCLoginPageVC.h"
#import "GloableEngineService.h"
#import "RCLoginView.h"
#import "RCBalanceVC.h"
#import "RCCostDetailVC.h"
#import "RCModifyPasswordVC.h"
#import "AppDelegate.h"
#import "NSString+NSString_Ex_.h"

#import "RCUserPhoneRegVC.h"
#import "RCUserEmailRegVC.h"
#import "RCTagsController.h"

#import "RCUserVC.h"



#define  kUserImageFile @"icon_user_left_logo"
@interface RCLoginPageVC ()<RCLoginProtocol>{
    RCLoginView     *_loginView;
    BOOL            _isTmpLogin;
//    RCUserInfoView  *_userInfo;
}

@end

@implementation RCLoginPageVC
@synthesize delegate = _delegate;


- (id) initWithTmpLogin
{
    self = [super init];
    if (self) {
        _isTmpLogin = YES;
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    [_loginView release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if (_isTmpLogin) {
        self.titleLabelText = @"登录";
    }else{
        self.titleLabelText = @"我的";
    }
    self.navigationItem.titleView = [self createTitleView];
    
    if (_isTmpLogin) {
        self.navigationItem.leftBarButtonItem = [self createBackBarButtonItem];
    }else{
        UIButton *signalButton = [UIButton buttonWithType:UIButtonTypeCustom];
        signalButton.frame = CGRectMake(0, 0, 44, 44);
        [signalButton setImage:[UIImage imageNamed:kUserImageFile]
                      forState:UIControlStateNormal];
        [signalButton setImage:[UIImage imageNamed:kUserImageFile]
                      forState:UIControlStateHighlighted];
        self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:signalButton];
    }
  
    
}

-(void) backBarButtonItemPressed:(id) sender {
    if (_isTmpLogin) {
        [self dismissModalViewControllerAnimated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([[GloableEngineService sharedInstance] hasLogin]) {
        [self loginSuccess];
    }else{
        [self showLoginView];
        [[GloableEngineService sharedInstance] getUserEmailName];
        NSString *userName = [GloableEngineService sharedInstance].userEmailName;
        if ([NSString stringIsNullOrEmpty:_loginView.userNameLabel.text]) {
            _loginView.userNameLabel.text = userName;
        }
        
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark subview 切换

- (void) showLoginView
{
    [self removeSubViews];
    if (_loginView == nil) {
        _loginView = [[RCLoginView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
    }
    _loginView.delegate = self;
    [self.viewContainer addSubview:_loginView];
}

- (void) removeSubViews
{
    if (_loginView.superview != nil) {
        [_loginView removeFromSuperview];
    }
}


- (void) loginSuccess
{
    if (_isTmpLogin) {
        [self dismissModalViewControllerAnimated:YES];
        return;
    }
    _loginView.userPassword.text = nil;
    if ([_delegate respondsToSelector:@selector(loginSuccess)]) {
        [_delegate loginSuccess];
    }
    
//    [self showUserInfoView];
}

- (void) loginFail
{
    [RCAppDelegate alertWithCustomizeString:@"登录失败"];

}


//注册：手机注册和邮箱注册
- (void) regBtnClick
{
    RCUserPhoneRegVC *vc1 = [[RCUserPhoneRegVC alloc] init];
    RCUserEmailRegVC *vc2 = [[RCUserEmailRegVC alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    
    NSArray *t = [NSArray arrayWithObjects:nav1,nav2, nil];
    [nav1 release], [nav2 release];
    [vc1 release],[vc2 release];
    
    RCTagsController *tagVC = [[RCTagsController alloc] initWithViewControllers:t];
    tagVC.hidesBottomBarWhenPushed = YES;
//    [self.parentViewController.navigationController pushViewController:tagVC animated:YES];
    [self.navigationController pushViewController:tagVC animated:YES];
    [tagVC release];
}

//#pragma mark 按钮点击

//- (void) checkBalance
//{
//    RCBalanceVC *balance = [[RCBalanceVC alloc] init];
//    balance.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:balance animated:YES];
//    [balance release];
//}
//
//- (void) checkDetailCost
//{
//    RCCostDetailVC *deatilVC = [[RCCostDetailVC alloc] init];
//    deatilVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:deatilVC animated:YES];
//    [deatilVC release];
//    
//}
//
//- (void) modifyPassword
//{
//    RCModifyPasswordVC *modify = [[RCModifyPasswordVC alloc] init];
//    modify.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:modify animated:YES];
//    [modify release];
//}

//- (void) logoutAccount
//{
//    [self showLoginView];
//    _loginView.userPassword.text = @"";
//    if ([NSString stringIsNullOrEmpty:_loginView.userNameLabel.text]) {
//        [[GloableEngineService sharedInstance]getUserEmailName];
//        NSString *userName = [GloableEngineService sharedInstance].userEmailName;
//        _loginView.userNameLabel.text = userName;
//    }
//    [[GloableEngineService sharedInstance]cleanLoginInfo];
//}
@end
